<?php
namespace Hawk\Model;
use Think\Model;
use Think\Model\RelationModel; 
class PositionModel extends RelationModel 
{
	protected $insertFields = array('cus_id','stock_code','stock_name','sale_price','position_num','source');
	protected $updateFields = array('id','cus_id','stock_code','stock_name','sale_price','position_num','source');
	protected $_validate = array(
		array('stock_code', 'require', '股票代码不能为空！', 1, 'regex', 3),
		array('stock_name', 'require', '股票名称不能为空！', 1, 'regex', 3),
		array('sale_price', 'require', '股票价格不能为空！', 1, 'regex', 3),
		array('position_num', 'require', '股票数量不能为空！', 1, 'regex', 3),
		array('source', 'require', '股票推荐类型不能为空！', 1, 'regex', 3)
		);
	protected function _before_insert(&$data, $option){
		$model=M('StockLog');
		$re = $model->where(array(
				'stock_code'=>array('eq',$data['stock_code']),
				'cus_id'=>array('eq',$data['cus_id'])
			))->select();
		if(count($re)==1){
			if(($re[0]['position_num']+$data['position_num'])<0){
				
				hecho("<font color='red'>操作后股票持仓将为负，请核查操作数量！</font><a href='javascript:history.back(-1);'>返回上一页</a>");
				exit;
			}
		}
		$data['user_id']=session('id');
		$data['addtime']=time();
		$data['updatetime']=time();
	}
	protected function _after_insert(&$data, $option){
		$model=M('StockLog');
		$re = $model->where(array(
				'stock_code'=>array('eq',$data['stock_code']),
				'cus_id'=>array('eq',$data['cus_id'])
			))->select();
		if(count($re)==1){
			//股票价值
			$sale_price = $re[0]['sale_price']*$re[0]['position_num']+$data['sale_price']*$data['position_num'];
			//持仓
			$data1['position_num'] = $re[0]['position_num']+$data['position_num'];
			//成本 
			if($data1['position_num']==0){
				$data1['sale_price'] =0;
			}else{
				$data1['sale_price'] = $sale_price/$data1['position_num'];
			}			
			$data1['updatetime']=time();
			//更新该股该客户持仓信息
			$model->where(array('id'=>array('eq',$re[0]['id'])))->data($data1)->save();
		}else{
			$data['updatetime']=time();
			$data['addtime']=time();
			$model->data($data)->add();
		}
	}
	protected function _before_update(&$data, $option){
		//修改前该次操作详情
		$stock_info = $this->find($option['where']['id']);
		//修改前此次操作资金额
		$stock_before_money = $stock_info['sale_price']*$stock_info['position_num'];
		//hecho($stock_before_money);
		//修改后此次操作金额
		$stock_after_money = $data['sale_price']*$data['position_num'];
		//修改前股票详情
		$model=M('StockLog');
		$re = $model->where(array(
				'stock_code'=>array('eq',$data['stock_code']),
				'cus_id'=>array('eq',$data['cus_id'])
			))->select();
		if(($re[0]['position_num']-$stock_info['position_num']+$data['position_num'])<0){
			hecho("<font color='red'>操作后股票持仓将为负，请核查操作数量！</font><a href='javascript:history.back(-1);'>返回上一页</a>");
			exit;
		}
		//修改后股票价值
		$sale_price = $re[0]['sale_price']*$re[0]['position_num']-$stock_before_money+$stock_after_money;
		//修改后股票持仓
		$data2['position_num'] =$re[0]['position_num']+$data['position_num']-$stock_info['position_num'] ;
		//修改后股票成本  
		if($data2['position_num']==0){
			$data2['sale_price'] =0;
		}else{
			$data2['sale_price'] = $sale_price/$data2['position_num'];
		}
		$data2['updatetime'] = time();
		//更新该股该客户持仓信息
		$model->where(array('id'=>array('eq',$re[0]['id'])))->data($data2)->save();
		//更新数据添加
		$data['user_id'] = session('id');
		$data['updatetime']=time();
		
	}
	protected function _before_delete($option){
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		//删除前该次操作详情
		$stock_info = $this->find($option['where']['id']);
		//删除前此次操作资金额
		$stock_before_money = $stock_info['sale_price']*$stock_info['position_num'];
		//修改前股票详情
		$model=M('StockLog');
		$re = $model->where(array(
				'stock_code'=>array('eq',$stock_info['stock_code']),
				'cus_id'=>array('eq',$stock_info['cus_id'])
			))->select();
		if(($re[0]['position_num']-$stock_info['position_num'])<0){
			hecho("<font color='red'>操作后股票持仓将为负，请核查操作数量！</font><a href='javascript:history.back(-1);'>返回上一页</a>");
			exit;
		}
		//修改后股票价值
		$sale_price = $re[0]['sale_price']*$re[0]['position_num']-$stock_before_money;
		//修改后股票持仓
		$data2['position_num'] =$re[0]['position_num']-$stock_info['position_num'] ;
		//修改后股票成本  
		if($data2['position_num']==0){
			$data2['sale_price'] =0;
		}else{
			$data2['sale_price'] = $sale_price/$data2['position_num'];
		}
		$data2['updatetime'] = time();
		//更新该股该客户持仓信息
		$model->where(array('id'=>array('eq',$re[0]['id'])))->data($data2)->save();
	}

}